package org.itboys.mobile.controller.web.information;

import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mdl.entity.mongo.information.MdlPortalItem;
import org.itboys.mobile.service.mongo.mobile.information.MdlPortalItemsService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created on 2017/12/1.
 * @author 朱坤涛
 * 资讯栏目管理
 */
@Controller
@RequestMapping("/Mdl/PortalItems")
public class MdlPortalItemsController extends BaseController{

    @Autowired
    private MdlPortalItemsService portalItemService;

    @RequestMapping("/toVM")
    public ModelAndView toVM(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("/portal/portal_item");
    }

    /**
     * 分页查询
     *
     * @param request
     * @param response
     */
    @RequestMapping("/list")
    public void list(HttpServletRequest request, HttpServletResponse response) {
        Map param = new HashMap();
        PageResult<MdlPortalItem> page = portalItemService.pageQuery(request,param);
        JsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
    }

    /**
     * 新增修改时保存
     *
     * @param request
     * @param response
     */
    @RequestMapping("/save")
    public void save(@ModelAttribute MdlPortalItem item, HttpServletRequest request, HttpServletResponse response) {
        try {
            if (item.getId() != 0) {
                portalItemService.updateExceptEmpty(item.getId(),item);
            } else {
                portalItemService.save(item);
            }
            AjaxUtils.renderText(response, "success");
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderText(response, "fail");
        }
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @param request
     * @param response
     */
    @RequestMapping("/findById")
    public void findById(@RequestParam Long id, HttpServletRequest request, HttpServletResponse response) {
        MdlPortalItem voucher = portalItemService.getById(id);
        AjaxUtils.renderJson(response, voucher);
    }

    /**
     * 删除
     *
     * @param id
     * @param request
     * @param response
     */
    @RequestMapping("/delete")
    public void delete(@RequestParam Long id, HttpServletRequest request, HttpServletResponse response) {
        try {
            portalItemService.delete(id);
            AjaxUtils.renderText(response, "success");
        } catch (Exception e) {
            AjaxUtils.renderText(response, "fail");
        }
    }

    /**
     * 获取所有资讯栏目列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getAll")
    public void getAll(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> sqlMap = new HashMap<String, Object>();
        List<MdlPortalItem> list = portalItemService.list(sqlMap);
        AjaxUtils.renderJson(response, list);
    }
}
